package src.main.com.example.springboot.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import src.main.com.example.springboot.common.Result;
import src.main.com.example.springboot.entity.Goods;
import src.main.com.example.springboot.mapper.GoodsMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;



@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    GoodsMapper goodsMapper;
    @PostMapping
    public Result<?> save(@RequestBody Goods goods){
        goodsMapper.insert(goods);
        return Result.success();
    }
    @PutMapping
    public Result<?>update(@RequestBody Goods goods){
        goodsMapper.updateById(goods);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        goodsMapper.deleteById(id);
        return Result.success();
    }
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids)
    {
        goodsMapper.deleteBatchIds(ids);
        return Result.success();
    }
    @RequestMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search
                              )

    {
        LambdaQueryWrapper<Goods> wrapper=Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Goods::getName,search);
        }
        Page<Goods> goodsPage=goodsMapper.findGoodsPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(goodsPage);
    }
}